/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package wf3.listener;

import acceso.util.Service;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.hibernate.HibernateException;
import wf3.core.Configuration;
import wf3.core.manager.DAOFactory;
import wf3.kernel.TransaManager;
import wf3.kernel.WorkflowFactory;

/**
 * Web application lifecycle listener.
 *
 * @author Rómulo Galindo Tanta
 */
public class WorkflowStartupListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("Levantando Workflow 2.1");

        try (InputStream in = sce.getServletContext().getResourceAsStream("/WEB-INF/config/acceso_produccion.properties")) {
            Properties accesoProperties = new Properties();
            accesoProperties.load(in);
            
            Configuration cfg = new Configuration(accesoProperties, sce.getServletContext());
            sce.getServletContext().setAttribute("CFG", cfg);

            WorkflowFactory.updateConfiguration(cfg);
            WorkflowFactory.StartupLog();
            WorkflowFactory.StartupDBConnection();
//            WorkflowFactory.StartupTransa();
            WorkflowFactory.StartupServices();
            //----------------------------------------------------------------
            WorkflowFactory.StartupGarbage();
            //----------------------------------------------------------------
            //Segundo hilo de solo transacciones de cierre
            TransaManager t = new TransaManager();
            t.start();
            //----------------------------------------------------------------
            //Da pie a controlar la salida de objetos dao
            DAOFactory.start();
            //----------------------------------------------------------------

            System.out.println("Iniciado WF 2.1 rev 525. ::EDPYME ACCESO CREDITICIO");

        } catch (Exception ep) {
            System.out.println("Error al momento de iniciar Workflow 2.1");
            ep.printStackTrace();
        }

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Deteniendo contexto Acceso");
        try {
            Service.detener();
            System.out.println("Cerrando contexto ACCESO-->Workflow shutdown!");
        } catch (Exception ex) {
            System.out.println("Error encontrado: " + ex);
        }

        try {
            WorkflowFactory.StopGarbage();
        } catch (Exception ex) {
            System.out.println("Error encontrado: " + ex);
        }

        try {
            System.out.println("Cerrando conexiones de base de datos");
            acceso.hibernate.AccesoHibernate.getSessionFactory().close();
            sce.getServletContext().removeAttribute("acceso");

        } catch (HibernateException ex) {
            System.out.println("Error encontrado: " + ex);
        }
    }
}
